<?php

/**
 * @deprecated Pouzi triedu Db_DbTable_* 
 *  
 * Commission
 *  
 * @author Radovan Dvorsky
 * @version 
 */

require_once 'Zend/Db/Table/Abstract.php';

class Db_Commission extends Zend_Db_Table_Abstract {
	
	/**
	 * The default table name 
	 */
	protected $_name = 'commission';
	
	/**
	 * Primary key
	 */
	protected $_primary = 'cid';
	
	/**
	 * Zavisle tabulky
	 */
	protected $_dependentTables = array('Db_Harmonogram','Db_Result','Db_ProfessorCommission');
	
	/**
	 * Vlozi udaje o komisii do databaze
	 *   
	 * $data format: 
	 * array(
	 * 	'name' => [meno komisie],
	 * 	'professors' => array(
	 * 		[id profesora 1],
	 * 		[id profesora 2],
	 * 		...
	 * 	)
	 * )
	 * 
	 * @param array $data
	 */
	public function addCommission(array $data){
		$adapter = $this->getAdapter();
		
		$adapter->beginTransaction();
		try{
			$lastCid = $this->insert(array(
				'name' => $data['name'],
			));
			
			$professorCommission = new Db_ProfessorCommission();
			
			foreach ($data['professors'] as $pid){
				$professorCommission->insert(array(
					'cid' => $lastCid,
					'pid' => $pid
				));
			}			
			$adapter->commit();			
		}catch(Zend_Db_Exception $e){
			$adapter->rollBack();			
			throw new Db_Exception($e->getMessage());
		}
	}
	
	/**
	 * Vrati vsetky komisie vratane clenov danej komisie (usporiadana podla mena komisie)
	 */
	public function fetchCommissions(){		
				
		$select = $this->getAdapter()->query(
				"SELECT `commission` . * , `professor` . * , professor_commission.role
				FROM `commission` 
				JOIN `professor_commission` ON commission.cid = professor_commission.cid
				JOIN `professor` ON professor.pid = professor_commission.pid
				ORDER BY commission.name,professor.lastname"
		);
		
		return $select->fetchAll();
	}
}